
package com.hfut.eeg.controller;

import com.hfut.eeg.common.api.CommonPage;
import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.common.util.GenerateCommon;
import com.hfut.eeg.dto.DoctorSearchResult;
import com.hfut.eeg.dto.DoctorUserInfo;
import com.hfut.eeg.service.SetupService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;


/**
 * 用来实现设置的Controller
 */
@RestController
@RequestMapping("/Setup")
public class SetupController {

    @Autowired
    SetupService setupService;

    @Autowired
    private GenerateCommon common;

    /**
     * 更新医生信息
     * @param doctorUserInfo
     * @return
     */
    @RequestMapping(value="/update",method=RequestMethod.POST)
    public CommonResult updateDoctorInfo(@RequestBody DoctorUserInfo doctorUserInfo)
    {
        int i = setupService.update(doctorUserInfo);
        if(i!=2)
            return CommonResult.failed();
        else
            return CommonResult.success(doctorUserInfo);
    }

    /**
     * 更新医生照片
     * @param doctorId
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/updateImg",method= RequestMethod.POST)
    public CommonResult updateImg(String doctorId, @RequestParam("file") MultipartFile file) throws IOException {
        Map<String,Object>map = setupService.updateImg(doctorId,file);
        if((Integer)map.get("value")==0)
            return CommonResult.failed();
        else {

            return CommonResult.success((String)map.get("imgHead"));
        }
    }


    /**
     * 添加新用户
     * @param doctorUserInfo
     * @return
     */
    @RequestMapping(value = "/addUser",method=RequestMethod.POST)
    public CommonResult addUser(@RequestBody DoctorUserInfo doctorUserInfo)
    {
        //System.out.println(doctorUserInfo);
        common.initID(setupService);
        doctorUserInfo.setDoctorId(common.generateID(setupService));
        int i = setupService.addDoctor(doctorUserInfo);
        if(i!=2)
            return CommonResult.failed();
        else
            return CommonResult.success(doctorUserInfo);

    }

    /**
     * 删除当前用户
     * @param doctorId
     * @return
     */
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    public CommonResult deleteUser(String doctorId)
    {
        int i = setupService.deleteDoctor(doctorId);
        if(i!=2)
            return CommonResult.failed();
        else
            return CommonResult.success(i);
    }

    /**
     * 删除其他用户
     * @param doctorUserInfo
     * @return
     */
    @RequestMapping(value = "/updateOtherUser",method = RequestMethod.POST)
    public CommonResult updateUser(@RequestBody DoctorUserInfo doctorUserInfo)
    {
        int i = setupService.updateOtherDoctor(doctorUserInfo);
        if(i!=2)
            return CommonResult.success(doctorUserInfo);
        else
            return CommonResult.failed();
    }

    /**
     * 获取指定医生信息
     * @param doctorId
     * @return
     */
    @RequestMapping(value = "/getDoctorUserInfo",method = RequestMethod.POST)
    public CommonResult  getDoctorUserInfo(String doctorId)
    {
        //System.out.println(doctorId);
        DoctorUserInfo doctorUserInfo = setupService.getDoctorUserInfo(doctorId);
        if(doctorUserInfo==null)
            return CommonResult.failed();
        else
            return CommonResult.success(doctorUserInfo);
    }

    /**
     * 进行分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping("/getDoctorInfoByPage")
    public CommonResult getDoctorInfoByPage(@RequestParam(value = "pageSize", defaultValue = "11")Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum)
    {
        List<DoctorSearchResult>results = setupService.getDoctorInfoByPage(pageSize,pageNum);
        if(results==null)
            return CommonResult.failed();
        else
            return CommonResult.success(CommonPage.restPage(results));
    }

    /**
     * 更新密码
     * @param doctorUserInfo
     * @return
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public CommonResult updatePassword(@RequestBody DoctorUserInfo doctorUserInfo)
    {
        int i = setupService.updatePassword(doctorUserInfo.getDoctorId(),doctorUserInfo.getPasswd());
        if(i==0)
            return CommonResult.failed();
        else
            return CommonResult.success(doctorUserInfo.getDoctorId());
    }

}